package cn.heyige.backend.controller;

import cn.heyige.backend.common.CommResponse;
import cn.heyige.backend.dto.SystemMonitorDto;
import cn.heyige.backend.facade.SystemMonitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("api/system")
@RequiredArgsConstructor
@Tag(name = "系统监控", description = "系统监控相关接口")
public class SystemMonitorController {
    
    private final SystemMonitorService systemMonitorService;
    
    /**
     * 获取系统状态信息
     */
    @GetMapping("status")
    @Operation(summary = "获取系统状态", description = "获取系统运行状态信息")
    public CommResponse<SystemMonitorDto.SystemStatusResponse> getSystemStatus() {
        SystemMonitorDto.SystemStatusResponse status = systemMonitorService.getSystemStatus();
        return CommResponse.ok(status);
    }
    
    /**
     * 获取AI服务连接状态
     */
    @GetMapping("aiStatus")
    @Operation(summary = "获取AI服务状态", description = "获取AI服务连接状态")
    public CommResponse<SystemMonitorDto.AiServiceStatusResponse> getAiServiceStatus() {
        SystemMonitorDto.AiServiceStatusResponse status = systemMonitorService.getAiServiceStatus();
        return CommResponse.ok(status);
    }
    
    /**
     * 获取系统统计信息
     */
    @GetMapping("statistics")
    @Operation(summary = "获取系统统计", description = "获取系统运行统计信息")
    public CommResponse<SystemMonitorDto.SystemStatisticsResponse> getSystemStatistics() {
        SystemMonitorDto.SystemStatisticsResponse statistics = systemMonitorService.getSystemStatistics();
        return CommResponse.ok(statistics);
    }
    
    /**
     * 获取健康检查结果
     */
    @GetMapping("health")
    @Operation(summary = "获取健康检查", description = "获取系统健康检查结果")
    public CommResponse<SystemMonitorDto.HealthCheckResponse> getHealthCheck() {
        SystemMonitorDto.HealthCheckResponse health = systemMonitorService.getHealthCheck();
        return CommResponse.ok(health);
    }
    
    /**
     * 获取系统完整信息（包含状态、AI服务状态、统计信息和健康检查）
     */
    @GetMapping("info")
    @Operation(summary = "获取系统完整信息", description = "获取系统完整信息（包含状态、AI服务状态、统计信息和健康检查）")
    public CommResponse<SystemMonitorDto.SystemInfoResponse> getSystemInfo() {
        SystemMonitorDto.SystemInfoResponse info = new SystemMonitorDto.SystemInfoResponse();
        info.setStatus(systemMonitorService.getSystemStatus());
        info.setAiServiceStatus(systemMonitorService.getAiServiceStatus());
        info.setStatistics(systemMonitorService.getSystemStatistics());
        info.setHealthCheck(systemMonitorService.getHealthCheck());
        info.setFetchTime(java.time.LocalDateTime.now());
        
        return CommResponse.ok(info);
    }
}